package com.example.component;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class CheckableListViewDemoActivity extends Activity {
	MyAdatper mAdapter;
	ListView lv;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		lv = new CheckableListView(this);
		setContentView(lv);
		mAdapter = new MyAdatper();
		lv.setAdapter(mAdapter);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add("checkable:" + (lv.getChoiceMode() == ListView.CHOICE_MODE_NONE));
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (lv.getChoiceMode() == ListView.CHOICE_MODE_NONE) {
			lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		} else {
			lv.setChoiceMode(ListView.CHOICE_MODE_NONE);
		}
		return super.onOptionsItemSelected(item);
	}

	class MyAdatper extends BaseAdapter {
		String[] data = new String[] {
				"A123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789",
				"B",
				"c",
				"A", "B", "c",
				"A", "B", "c",
				"A", "B", "c",
				"A", "B", "c",
				"A", "B", "c",
				"A", "B", "c",
				"A", "B", "c"
		};

		@Override
		public int getCount() {
			return data.length;
		}

		@Override
		public String getItem(int position) {
			return data[position];
		}

		@Override
		public long getItemId(int position) {
			return -1;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = null;
			if (convertView == null) {
				v = getLayoutInflater().inflate(R.layout.adapter_checkable_item, null);
			} else {
				v = convertView;
			}
			TextView text = (TextView) v.findViewById(R.id.text);
			text.setText(getItem(position));
			return v;
		}
	}
}
